package cn.ecnu.auction.service;



import org.json.JSONObject;

import cn.ecnu.auction.dao.BidDAO;
import cn.ecnu.auction.dao.GoodsDAO;
import cn.ecnu.auction.engine.IService;
import cn.ecnu.auction.engine.Context;
import cn.ecnu.auction.po.Goods;

public class GoodsService implements IService{
	
	public void executeService(Context context) throws Exception {
		
		JSONObject requestJson = context.getJsonRequestObject();
		String operation = requestJson.getString("operation");
		int returnValue = 0;
		String output = null;
		String jsonString = null;
		
		if(operation.equals("add")){
			//新增拍卖品
			String gname = requestJson.getString("gname");
			String description = requestJson.getString("description");
			double startPrice = Double.valueOf(requestJson.getDouble("startPrice"));
			String startTime = requestJson.getString("startTime");
			String lastingTime = requestJson.getString("lastingTime");
			int uid = Integer.valueOf(requestJson.getString("uid"));
			String type = requestJson.getString("type");
			
			returnValue = new GoodsDAO().addGood(gname, description, startPrice, startTime, lastingTime,uid,type);
		}
		else if(operation.equals("query")){
			JSONObject json = new JSONObject().put("goods", new GoodsDAO().getGoods());
			returnValue = 1;
			output=json.toString();
		}
		else if(operation.equals("querymore")){
			
			int gid = Integer.valueOf(requestJson.getString("gid"));
			JSONObject json =new GoodsDAO().getGoodsById(gid);
			if(json != null){
				returnValue = 1;
				output=json.toString();
			}
			else{
				returnValue =0;
				output="failed";
			}
			
		}
		else if(operation.equals("update")){
			int gid = Integer.valueOf(requestJson.getString("gid"));
			
			returnValue = new GoodsDAO().updateResult(gid);			
		}
		
		//返回值
		JSONObject o = new JSONObject();
		o.put("returnValue", returnValue);
		o.put("output", output);
		context.setJsonResponseObject(o);
	}
}
